package jers.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import jers.Callback;
import jers.JERS;
import jers.Job;
import jers.Result;
import jers.Service;

/**
 * Classe que implementa a interface {@link Service}.
 * <p>
 * Esta implementa um decorador para o servico do cliente para
 * poder obter a instancia proxy da callback do clientem, nao
 * sendo necessario que este conheca os detalhes da comunicacao.
 * </p>
 * 
 * @see {@link UnicastRemoteObject}, {@link SystemFactory}, {@link Callback}
 * 
 * @author 
 * 		<a href="mailto:felipe.zappala@gmail.com">
 * 			Luis Felipe Zappala
 * 		</a> [ <a href="http://lncc.br/ist">ISTCC-P</a> / 2009 ]
 */
final class RemoteServiceProxy implements Service {
	
	/** O ID serial da versao da classe */
	private static final long serialVersionUID = -6839015114494666509L;
	
	private Service delegateService;

	public RemoteServiceProxy(Service delegate) {
		if (delegate == null) {
			throw new IllegalArgumentException(
					"Validation error: parameter Service is null");
		}
		this.delegateService = delegate;
	}
	
	public void dispatch(Job job, Callback callback) throws RemoteException {
		try {
			Callback proxyCallback = JERS.factory.newCallback(callback);
			this.delegateService.dispatch(job, proxyCallback);
			
		} catch (Exception e) {
			throw new RemoteException("Service proxy error", e);
		}
	}

	public Result execute(Job job) throws RemoteException {
		try {
			return this.delegateService.execute(job);
			
		} catch (Exception e) {
			throw new RemoteException("Service proxy error", e);
		}
	}
	
}
